﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Geographical;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms.Users
{
    public partial class CreateUser : AdminUserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;

            if (!IsPostBack)
            {
                //List<CityInfo> _lstCities = GeographicalController.GetCities(1);
                //drCity.DataTextField = "Name";
                //drCity.DataValueField = "CityID";
                //drCity.DataSource = _lstCities;
                //drCity.DataBind();
            }

        }
        private string _lstUser;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            foreach (UserInfo userInfo in UserController.GetUsers())
            {
                _lstUser += SecurityUtility.MD5Encrypt(userInfo.Username) + ";";
                //_lstUser += userInfo.Username + ";";
                checkUser.Attributes.Add("onclick", "checkExistUser('" + _lstUser + "');");
            }
        }

        protected static bool UsernameIsExisted(string uname)
        {
            if (UserController.GetUser(uname) == null)
                return false;
            return true;
        }
        protected bool Validate()
        {
            bool ok = true;
            if (UsernameIsExisted(txtUsername.Text))
            {
                lblValidateAccount.Visible = true;
                lblValidateAccount.Text = "<span style='color:red'>Tên đăng nhập đã tồn tại!</span>";
                ok = false;
            }
            else
            {
                if (!CommonFunctions.IsUserName(txtUsername.Text))
                {
                    lblValidateAccount.Visible = true;
                    lblValidateAccount.Text = "<span style='color:red'>Tên đăng nhập ít nhất 4 kí tự trở lên!</span>";
                    ok = false;
                }
                else
                    lblValidateAccount.Visible = false;
            }

            return ok;
        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (Validate())
            {
                try
                {
                    UserInfo _user = new UserInfo();
                    _user.Username = txtUsername.Text.Trim();
                    _user.Password = SecurityUtility.MD5Encrypt(txtPassword.Text.Trim());

                    if (_user.Password != SecurityUtility.MD5Encrypt(txtPasswordConfirm.Text.Trim()))
                        return;
                    _user.Email = txtEmail.Text;
                    _user.Country = 1;
                    _user.FirstName = txtFirstName.Text;
                    _user.LastName = txtLastName.Text;
                    _user.AdditionalInfo = txtAdditionalInfo.Text;
                    _user.Address = txtAddress.Text;
                    _user.City = ConvertUtility.ToInt32(drCity.SelectedValue);
                    _user.Mobile = txtMobile.Text;
                    _user.Tel = txtTel.Text;
                    if (rdoGender.SelectedValue == "0")
                    {
                        _user.Gender = true;
                    }
                    else
                    {
                        _user.Gender = false;
                    }
                    _user.Birth = radBirth.SelectedDate;
                    _user.Avatar = string.Empty;
                    _user.Ym = txtYm.Text;
                    _user.Cmtnd = txtCmtnd.Text;
                    _user.Skype = txtSkype.Text;
                    _user.IsSuperUser = false;
                    _user.IsAdmin = true;
                    _user.Status = true;
                    _user.Birth = radBirth.SelectedDate;
                    _user.UserID = UserController.CreateUser(_user);
                    UserController.AddUserToPortal(_user.UserID, CurrentPortalID);
                    // Insert Cores_Audits
                    
                    ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                    AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Create, CurrentCommand.CtrlKey, CurrentUser.UserID, _user.UserID
                                                , _user.Username, 1, DateTime.Now);
                    //End
                    Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { }, new string[] { }));

                }
                catch (Exception ex)
                {
                    lblUpdateStatus.Text = (ex.Message);
                }
            }
        }


        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { }, new string[] { }));
        }
    }
}